package com.burgeon.backend.EnterpriseWeChat.rest.wx;

import com.burgeon.backend.EnterpriseWeChat.rest.base.RESTBase;
import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindVIPDataByWCcode extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindVIPDataByWCcode(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public String getData(String WXCODE) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(WXCODE), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception();
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        String ID = null;
        try {
            ID = jsonArray.getJSONArray(0).getString(0);
        } catch (JSONException ignored) {

        }


        return ID;
    }

    private MultiValueMap Data(String WXCODE) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("ID");


        params.put("columns", colums);
        params.put("table", "C_VIP");//表名

        params.put("count", "true");

        HashMap paramss = new HashMap();
        paramss.put("column", "'WXCODE'");
        paramss.put("condition", "'"+WXCODE+"'");

        HashMap orderby = new HashMap();

        orderby.put("column","ID");
        orderby.put("asc","false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindVIPDataByWCcode g = new FindVIPDataByWCcode("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(g.getData("54a55b6u"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
